;=================================================
; macros sample
;


;=================================================
; do/dolist/dotimes sample
; :DOC:
; (do (variable-definition*)
;    (end-test-form result-form*)
;  statement*)
;
; here:
;   variable-definition: (var init-form step-form)
(defun do-stuff-sample ()

 ; print 4 numbers
 (do ((i 0 (1+ i)))
  ((>= i 4))
  (format t "~a " i)
  ) ; do
 (format t "~%")

 ; the same thing w/dotimes
 ;(dotimes (i 4) (print i))


 ; bodiless fibonacci computing loop
 (format t "fib = ~a~%"
 (do ((n 0 (1+ n))
      (cur 0 next)
      (next 1 (+ cur next)))
  ((= 10 n) cur)
  ) ; do
 ) ; format

 ; a very simple do constuction which simply imitates for loop
 (format t "~%for loop imitation => ~a~%"
 (do ((num 2 (1+ num)))
     ((> num 9) (+ num 5)) ; the last element in the condition list will be returned. If there is no such an element - NIL will be returned (last execution of the condition shall be NIL because of exit from the loop).
     (format t "num = ~a~%" num)
 ) ; do
 ) ; format

 ; idiomatic do which collects numbers into a list
 (setf list-collected
 (do ((nums nil) (i 1 (1+ i)))
      ((> i 10) (nreverse nums))
      (push i nums)
 ) ; do
 ) ; setf
 (format t "list = ~a~%" list-collected)

 ) ; do-stuff-sample

;=================================================
; loop test
; :DOC:
; (loop body-form*)
;
; The symbols across, and, below, collecting, counting, finally, for,
; from, summing, then, and to are some of the loop keywords whose
; presence identifies these as instances of the extended LOOP.
;
(defun loop-stuff-test ()
 ; using loop to create a numbers list (see a similar sample above)
 (format t "list w/loop = ~a~%"
 (loop for i from 1 to 10 collecting i)
 )

 ; using loop to calculate the number of vowels in the string
 (format t "the number of vowels is ~a~%"
 (loop for x across "the quick brown fox jumps over the lazy dog"
      counting (find x "aeiou"))
 )

 ; this computes the 11th Fibonacci number similar to the DO loop used earlier
 (format t "fib w/loop = ~a~%"
 (loop for i below 10
       and a = 0 then b
       and b = 1 then (+ b a)
       finally (return a))
 )

 ) ; loop-stuff-test

;=================================================
; macros test
;
(defun test ()
 ; 1
 (do-stuff-sample)

 ; 2
 (loop-stuff-test)
 )

;=================================================
; app entry point
;
(defun main ()

 (format t "macros sample~%=====~%")

 (test)

 (exit)
 )

;=================================================
; executes main
;
(main)

